package com.hh.core.config;

import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import com.hh.core.property.DataSourceProperty;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

/**
 * 
* @ClassName: HikariCPConfig  
* @Description: TODO(HikariCP 连接池配置)  
* @author hxn
* @date 2018年1月16日 下午4:26:08  
*
 */
@Configuration
public class HikariCPConfig {
	
	@Bean(destroyMethod = "close")
	@Primary
	public DataSource dataSource(DataSourceProperty dataSourceProperty){
		HikariConfig hikariConfig = new HikariConfig();
		hikariConfig.setDriverClassName(dataSourceProperty.getDriverClassName());
		hikariConfig.setJdbcUrl(dataSourceProperty.getUrl());
		hikariConfig.setUsername(dataSourceProperty.getUsername());
		hikariConfig.setPassword(dataSourceProperty.getPassword());

		hikariConfig.setMaximumPoolSize(5);
		hikariConfig.setConnectionTestQuery("SELECT 1 from dual");
		hikariConfig.setPoolName("springHikariCP");

		hikariConfig.addDataSourceProperty("dataSource.cachePrepStmts", "true");
		hikariConfig.addDataSourceProperty("dataSource.prepStmtCacheSize", "250");
		hikariConfig.addDataSourceProperty("dataSource.prepStmtCacheSqlLimit", "2048");
		hikariConfig.addDataSourceProperty("dataSource.useServerPrepStmts", "true");

		HikariDataSource dataSource = new HikariDataSource(hikariConfig);

		return dataSource;
	}
}
